package com.ltmonitor.web.controller.mobile;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ltmonitor.entity.UserInfo;
import com.ltmonitor.service.IUserInfoService;
import com.ltmonitor.util.DateUtil;
import com.ltmonitor.web.controller.GenericAction;
import com.ltmonitor.web.util.JsonMessage;
import com.ltmonitor.web.util.OnlineUser;

@Controller
@RequestMapping("/mobile")
public class MobileUserAction extends GenericAction {


	@Autowired
	private IUserInfoService userInfoService;
	@ResponseBody
	@RequestMapping("/onlineUserList.action")
	public JsonMessage onlineUserList() {
		try {
			Collection<UserInfo> userList = MobileOnlineUser
					.getOnlineUserList();
			ArrayList<Map> users = new ArrayList();
			List<Long> userIdList = new ArrayList<Long>();
			for (UserInfo u : userList) {
				userIdList.add(u.getEntityId());
			}
			OnlineUser u = this.getOnlineUser();
			if(u == null){
				return json(false,"会话过期，请重新登录");
			}
			String queryId = "selectUserGpsRealData";
			Map params = new HashMap();
			long depId = u.getCompanyId();
			if(depId > 0)
			{
			    List<Long> depIdList = this.departmentService.getDepIdList(depId);
			    params.put("depIdList", depIdList);
			}
			//params.put("userIdList", userIdList);
			Date today = new Date();
			Date date = DateUtil.getDate(today, Calendar.MINUTE, -30);
			params.put("sendTime", date);
			List result = this.queryService.query(queryId,params);
			return json(true, result);
		} catch (Exception ex) {
			log.error(ex.getMessage());
			log.error(ex.getStackTrace());
			return json(false, ex.getMessage());
		}
	}

	/**
	 * 设置地图中心
	 * 
	 * @return
	 */

	@ResponseBody
	@RequestMapping("/setMapCenter.action")
	public JsonMessage setMapCenter(double lat, double lng, int zoom,
			String mapType) {
		try {
			OnlineUser onlineUser = this.getOnlineUser();
			if (onlineUser != null) {
				UserInfo user = (UserInfo) this.userInfoService.load(
						onlineUser.getEntityId());
				user.setMapCenterLng(lng);
				user.setMapCenterLat(lat);
				user.setMapLevel(zoom);
				this.userInfoService.saveOrUpdate(user);// 保存到用户信息中
				return json(true, "已设置为当前默认地图中心！");
			}
			return json(false, "会话过期，请重新登录!");
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			return json(false, e.getMessage());
		}

	}

	@ResponseBody
	@RequestMapping("/updatePassword.action")
	public JsonMessage updatePassword(String oldPsd, String newPsd) {
		OnlineUser onlineUser = this.getOnlineUser();
		if (onlineUser != null) {
			UserInfo user = (UserInfo) this.userInfoService.load(
					onlineUser.getEntityId());
			if (user.getPassword().equals(oldPsd)) {
				user.setPassword(newPsd);
				this.userInfoService.saveOrUpdate(user);// 保存到用户信息中
				return json(true, "修改成功");
			} else {
				return json(false, "旧密码不正确");

			}
		}
		return json(false, "会话过期，请重新登录!");
	}


}
